package com.xing.controller;

import com.google.gson.Gson;
import com.xing.dao.CommonDao;
import com.xing.model.HsdDataResearch;
import com.xing.model.HsdModelSurveyListObj;
import com.xing.model.HsdQuestionAndAnswerModelListObj;
import com.xing.model.SuperObject;
import com.xing.my_model.HsdAccountObject;
import com.xing.util.HsdProgressDialog;
import com.xing.util.MyExecutorService;
import com.xing.util.MyLog;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;



public class CommonController extends CommonBaseController {

	private static CommonController instance = null;

	public static CommonController getInstance() {
		if (instance == null) {
			instance = new CommonController();
		}
		return instance;
	}
	
	
	public void getQuestionAndAnswerModelList(final String surveyuuids,final String username,final String token,final Handler handler,final Context context){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdQuestionAndAnswerModelListObj obj  = null;
				try {
					obj = CommonDao.getQuestionAndAnswerModelList(surveyuuids, username, token, context);
				} catch (Exception e) {
					// TODO: handle exception
					obj = null;
					MyLog.outLogDetail(e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(obj, context, handler);
//					dimissProgressDialog(progressDialog);
				}
			}
		});
	}
	
	
	public void getHsdModelSurveyListObj(final String surveyuuids,final String username,final String token,final Handler handler,final Context context){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdModelSurveyListObj obj  = null;
				try {
					obj = CommonDao.getHsdModelSurveyListObj(surveyuuids, username, token, context);
				} catch (Exception e) {
					// TODO: handle exception
					obj = null;
					MyLog.outLogDetail(e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(obj, context, handler);
				}
			}
		});
	}
	
	public void upLoadHsdResult(final Context context,final String resultkey,final String resultValue,final Handler handle){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				SuperObject result  = null;
				try {
					result = CommonDao.upLoadHsdResult(context,resultkey, resultValue);
				} catch (Exception e) {
					// TODO: handle exception
					result = null;
					MyLog.outLogDetail(e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					Message message = new Message();
					Bundle data = new Bundle();
					data.putSerializable("result", result);
					Gson gson = new Gson();
					HsdDataResearch hsdDataResearch = gson.fromJson(resultkey, HsdDataResearch.class);
					data.putSerializable("hsd_data_research", hsdDataResearch);
					message.setData(data);
					sendMyMessage(message, context, handle);
//					dimissProgressDialog(progressDialog);
				}
			}
		});
	}
	
	public void login(final Context context,final String user_name,final String passoword,final String deviceid,final Handler handler){
		HsdProgressDialog.getProgressDialog(context);
		MyExecutorService.getExecutorService().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HsdAccountObject hsdAccountObject  = null;
				try {
					hsdAccountObject = CommonDao.login(context, user_name, passoword, deviceid);
				} catch (Exception e) {
					// TODO: handle exception
					hsdAccountObject = null;
					MyLog.outLogDetail(e.getMessage());
				}finally{
					HsdProgressDialog.dismissProgressDialog(context);
					sendMessage(hsdAccountObject, context, handler);
				}
			}
		});
	}


}
